package com.borya.dao.impl;

import java.util.List;
import org.apache.log4j.Logger;
import com.borya.framework.annotation.Repository;
import com.borya.framework.core.ServerSqlSession;
import com.borya.framework.core.ServerSqlSessionFactory;

public abstract class BaseDAOImpl extends BaseRedisImpl{

	private Logger log = Logger.getLogger(getClass());
	
	@Repository("/conf/sql.properties")
	private ServerSqlSessionFactory sqlDataSource ;
	
	public ServerSqlSessionFactory getSqlDataSource() {
		return sqlDataSource;
	}
	
//	@Deprecated
//	public Connection getConnection(){
//		try {
//			return SessionUtil.getInstance().getServerSqlSession().connection();
//		} catch (Exception e) {
//			log.error("连接数据库失败",e);
//		}
//		return null;
//	}
	
	public boolean executeUpdate(String sql){
		try (ServerSqlSession session = getSqlDataSource().session();){
			return session.executeUpdate(sql)>0;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}
	
	public boolean executeUpdate(String sql, Object... params){
		try (ServerSqlSession session = getSqlDataSource().session();){
			return session.executeUpdate(sql,params)>0;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
	}
	
	public int[] executeBatch(List<String> sql_list) {
		try (ServerSqlSession session = getSqlDataSource().session();){
			return session.executeBatch(sql_list);
		} catch (Exception ex) {
			log.error(ex.getMessage(),ex);
			return new int[]{};
		}
	}
	
}